package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Root;
import com.sl.au.entity.User;
import com.sl.au.entity.Weekly;
import com.sl.au.repository.WeeklyRepository;
import com.sl.au.service.OperateLogService;
import com.sl.au.service.WeeklyService;

@Service("weeklyService")
@Transactional
public class WeeklyServiceImpl extends BaseServiceImpl implements WeeklyService {

	@Autowired
	WeeklyRepository weeklyRepository;

	@Autowired
	OperateLogService operateLogService;

	@Override
	public List<Weekly> getPage(int start, int length, Map<String, String> condition, String key, String dataFilter,
			String orderName, Direction dir) {
		Page<Weekly> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (key == null) {
			key = "";
		} else {
			key = key.replaceAll("\\s+", "");
		}
		if (dataFilter.equals("所有")) {
			if(condition.get("isInstructions")!="")
				pages = weeklyRepository.findAllWeekly(key, condition.get("isInstructions"), condition.get("userId"), pageable);
			else
				pages = weeklyRepository.findAllWeekly(key, condition.get("userId"), pageable);
		} else if (dataFilter.equals("本部门")) {
			if(condition.get("isInstructions")!="")
				pages = weeklyRepository.findWeekly1(key, condition.get("isInstructions"), condition.get("superior"),  pageable);
			else
				pages = weeklyRepository.findWeekly(key, condition.get("superior"),  pageable);
		} else if (dataFilter.equals("本人")) {
			if(condition.get("isInstructions")!="")
				pages = weeklyRepository.findWeeklyForMine(key, condition.get("isInstructions"), condition.get("userId"), pageable);
			else
				pages = weeklyRepository.findWeeklyForMine(key, condition.get("userId"), pageable);
		} else if (dataFilter.equals("本片区")) {
			if(condition.get("isInstructions")!="")
				pages = weeklyRepository.findWeekly1(key, condition.get("isInstructions"), condition.get("areaId"), condition.get("userId"), pageable);
			else
				pages = weeklyRepository.findWeekly(key, condition.get("areaId"), condition.get("userId"), pageable);
		}else if (dataFilter.equals("本科室")) {
			if(condition.get("isInstructions")!="")
				pages = weeklyRepository.findSectionWeekly(key, condition.get("isInstructions"), condition.get("sectionId"), condition.get("userId"), pageable);
			else
				pages = weeklyRepository.findSectionWeekly(key, condition.get("sectionId"), condition.get("userId"), pageable);
		}
		return pages.getContent();
	}
	@Override
	public List<Weekly> getPageToMobile(int start, int length, Map<String, String> condition, String key, String dataFilter,
			boolean published,String orderName, Direction dir) {
		Page<Weekly> pages = null;
		Pageable pageable=null;
		if(published)
			pageable = new PageRequest(start / length, length, dir, orderName);
		else
			pageable = new PageRequest(start / length, length,  dir,"createTime");
		if (key == null) {
			key = "";
		} else {
			key = key.replaceAll("\\s+", "");
		}
		if (dataFilter.equals("所有")) {
			pages = weeklyRepository.findAllWeekly(key, condition.get("userId"),published, pageable);
		} else if (dataFilter.equals("本部门")) {
			pages = weeklyRepository.findWeekly(key, condition.get("superior"), published, pageable);
		} else if (dataFilter.equals("本人")) {
			pages = weeklyRepository.findWeeklyForMine(key, condition.get("userId"),published, pageable);
		} else if (dataFilter.equals("本片区")) {
			pages = weeklyRepository.findWeekly(key, condition.get("areaId"), condition.get("userId"), published, pageable);
		}else if (dataFilter.equals("本科室")) {
			pages = weeklyRepository.findSectionWeekly(key, condition.get("sectionId"), condition.get("userId"), published, pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getCount(int start, int length, Map<String, String> condition,String dataFilter) {
		long count = 0;
		if (dataFilter.equals("所有")) {
			if(condition.get("isInstructions")!="")
				count = this.getRepository().findAllWeeklyCount(condition.get("key"), condition.get("isInstructions"), condition.get("userId"));
			else
				count = this.getRepository().getCountForAll(condition.get("key"), condition.get("userId"));
		} else if (dataFilter.equals("本部门")) {
			if(condition.get("isInstructions")!="")
				count = this.getRepository().findWeekly1Count(condition.get("key"), condition.get("isInstructions"), condition.get("superior"));
			else
				count = this.getRepository().getCountWithSuperior(condition.get("key"), condition.get("superior"));
		} else if (dataFilter.equals("本片区")) {
			if(condition.get("isInstructions")!="")
				count = this.getRepository().findWeekly1Count(condition.get("key"), condition.get("isInstructions"), condition.get("areaId"), condition.get("userId"));
			else
				count = this.getRepository().getCount(condition.get("key"), condition.get("areaId"),condition.get("userId"));
		} else if (dataFilter.equals("本科室")) {
			if(condition.get("isInstructions")!="")
				count = this.getRepository().findSectionWeeklyCount(condition.get("key"), condition.get("isInstructions"), condition.get("sectionId"), condition.get("userId"));
			else
				count = this.getRepository().getSectionCount(condition.get("key"), condition.get("sectionId"), condition.get("userId"));
		}else if (dataFilter.equals("本人")) {
			if(condition.get("isInstructions")!="")
				count = this.getRepository().findWeeklyForMineCount(condition.get("key"), condition.get("isInstructions"), condition.get("userId"));
			else
				count = this.getRepository().getCount(condition.get("key"), condition.get("userId"));
		}
		return count;
	}

	@Override
	public long getCountMobile(int start, int length, Map<String, String> condition, String dataFilter,
			boolean published) {
		long count = 0;
		if (dataFilter.equals("所有")) {
			count = this.getRepository().getCountForAll(condition.get("key"), condition.get("userId"),published);
		} else if (dataFilter.equals("本部门")) {
			count = this.getRepository().getCountWithSuperior(condition.get("key"), condition.get("superior"),published);
		} else if (dataFilter.equals("本片区")) {
			count = this.getRepository().getCount(condition.get("key"), condition.get("areaId"),condition.get("userId"),published);
		} else if (dataFilter.equals("本科室")) {
			count = this.getRepository().getSectionCount(condition.get("key"), condition.get("sectionId"), condition.get("userId"),published);
		}else if (dataFilter.equals("本人")) {
			count = this.getRepository().getCount(condition.get("key"), condition.get("userId"),published);
		}
		return count;
	}
	public WeeklyRepository getRepository() {
		return this.weeklyRepository;
	}

	@Override
	public void patch(Weekly weekly) {
		Weekly w = this.weeklyRepository.getOne(weekly.getId());
		if (null == w)
			return;
		BeanUtils.copyProperties(weekly, w, "issueDate", "createTime", "issuerName", "issuer");
		if(w.getIssueDate()==null&&w.isPublished()){
			w.setIssueDate(new Date());
		}
		this.weeklyRepository.save(w);
		String content = "编辑周报【" + weekly.getName() + "】";
		User user = weekly.getIssuer();
		operateLogService.log(user, "编辑周报", content);
	}

}
